#ifndef FILESERVER_H
#define FILESERVER_H

#include <QObject>
#include <QTcpServer>
#include <QAbstractSocket>
#include <QTcpSocket>
#include <QMap>
#include <QString>
#include <QThread>
#include "widget.h"


class FileServer : public QTcpServer
{
    Q_OBJECT
public:
    explicit FileServer(QString param_server_name,quint8 param_thread_count=1,QObject *parent = nullptr);
    ~FileServer();

    QString getServername() {return m_server_name;}     //获取服务器名字
    quint8 getThreadCount() {return m_thread_count;}    //获取线程数量
    //获取当前每个线程的活跃socket数量
    void threadMonitor(QMap<qint32,quint32>& param_info);

signals:
    void mynewConnection(qintptr socketDescriptor);

public slots:
    //开始监听
    bool run(quint16 port);
    void socketClose(qint32 id);

protected:
    //有新的socket连接到来时
    void incomingConnection(qintptr socketDescriptor);

    struct ThreadInfo //线程信息，包括标志线程的ID和活跃socket的数量
    {
        qint32 id;      //线程id
        QThread msg_thread;     //线程
        quint32 active_count;   //活跃线程数量
    };
    //获取活跃socket数量最小的线程id
    qint32 getMinActiveThread();



private:
    QString m_server_name;
    quint8 m_thread_count;  //线程数量
    ThreadInfo* m_socket_thread;    //线程数组

};

#endif // FILESERVER_H
